package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.HttpStatusEnum;
import com.example.entity.ResponseData;
import com.example.pojo.Medicine;
import com.example.mapper.MedicineMapper;
import org.springframework.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 药物信息表(Medicine)表服务实现类
 *
 * @email: pengyujun53@163.com
 * @author: peng_YuJun
 * @date: 2024/01/17
 * @time: 23:13
 */
@Service
public class MedicineService{
    /**
     * mapper对象 
     */
    @Resource
    private MedicineMapper medicineMapper;
    
    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @param medicine 筛选条件
     * @return
     */
    public ResponseData getByPage(Integer currentPage, Integer pageSize, Medicine medicine) {
        LambdaQueryWrapper<Medicine> lqw = new LambdaQueryWrapper<>();
        lqw.like(medicine.getId() != null, Medicine::getId, medicine.getId());
        lqw.like(medicine.getTitle() != null, Medicine::getTitle, medicine.getTitle());
        lqw.eq(!Arrays.asList(null, -1).contains(medicine.getDeleted()), Medicine::getDeleted, medicine.getDeleted());
        IPage ipage = new Page(currentPage,pageSize);
        medicineMapper.selectPage(ipage,lqw);
        //若当前页码大于总页面数
        if (currentPage > ipage.getPages()){
            ipage = new Page(ipage.getPages(),pageSize);
            medicineMapper.selectPage(ipage,lqw);
        }
        if (ipage.getTotal()<=0){
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(),ipage);
    }
    
    /**
     * 根据ID查询信息
     *
     * @param id 主键
     * @return 单条数据
     */
    public ResponseData findById(Long id) {
        Medicine medicine = medicineMapper.selectById(id);
        if(medicine == null || medicine.getId() == null){
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        //返回结果
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(),medicine);
    }
    
    /**
     * 新增数据
     *
     * @param  medicine 实体
     * @return 新增结果
     */
    public ResponseData add(Medicine medicine) {
        //插入
        if (medicineMapper.insert(medicine)>0){
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "新增失败，请稍后再试");
    }
    
    /**
     * 更新数据
     *
     * @param medicine 实体
     * @return 更新结果
     */
    public ResponseData update(Medicine medicine) {
        //更新
        if (medicineMapper.updateById(medicine)>0){
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "修改失败，请稍后再试");
    }
    
    /**
     * 删除数据
     *
     * @param id 主键
     * @return 
     */
    public ResponseData delete(Long id) {
        if (medicineMapper.deleteById(id)>0){
            //返回成功响应
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "删除失败，请稍后再试");
    }
    
    /**
     * 批量删除
     * @param idList
     * @return
     */
    public ResponseData deleteBatch(List<Long> idList){
        if (medicineMapper.deleteBatchIds(idList)>0){
            //返回成功响应
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "删除失败，请稍后再试");
    }

    /**
     * 查询有效的药品信息
     * @return
     */
    public ResponseData findAllValid() {
        LambdaQueryWrapper<Medicine> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Medicine::getDeleted, 0);
        List<Medicine> medicines = medicineMapper.selectList(lqw);
        if(medicines == null || medicines.isEmpty()){
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        //返回结果
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(),medicines);
    }

    /**
     * 统计药品数
     * @return
     */
    public ResponseData countAll() {
        Long all = medicineMapper.selectCount(null);
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), all);
    }
}
